# Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

# Do not use it, use `flare/rpc:protobuf` instead.

cc_library(
    name = "protobuf",
    hdrs = [
        "rpc_channel.h",
        "rpc_client_controller.h",
        "rpc_server_controller.h",
    ],
    visibility = [
        "//flare/rpc:__pkg__",
        "//flare/testing:__pkg__",
    ],
    deps = [
        ":baidu_std_protocol",
        ":poppy_protocol",
        ":proto_over_http_protocol",
        ":qzone_protocol",
        ":rpc_channel",
        ":rpc_client_controller",
        ":rpc_meta_cc_proto",
        ":rpc_server_controller",
        ":service",
        ":service_method_locator",
        ":std_protocol",
    ],
)

proto_library(
    name = "rpc_meta_proto",
    srcs = ["rpc_meta.proto"],
)

cc_proto_library(
    name = "rpc_meta_cc_proto",
    visibility = ["//visibility:public"],
    deps = [":rpc_meta_proto"],
)

cc_library(
    name = "binlog",
    srcs = ["binlog.cc"],
    hdrs = ["binlog.h"],
    deps = [
        ":message",
        "//flare/rpc/binlog:packet_desc",
    ],
)

cc_test(
    name = "binlog_test",
    srcs = ["binlog_test.cc"],
    deps = [
        ":binlog",
        ":message",
        "//flare/testing:message_cc_proto",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "message",
    srcs = ["message.cc"],
    hdrs = ["message.h"],
    visibility = [
        "//flare/rpc/binlog/gdt:__pkg__",
    ],
    deps = [
        ":rpc_meta_cc_proto",
        "//flare/base:buffer",
        "//flare/base:enum",
        "//flare/base:maybe_owning",
        "//flare/base:string",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:message",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "message_test",
    srcs = ["message_test.cc"],
    deps = [
        ":message",
        "//flare/testing:main",
        "//flare/testing:message_cc_proto",
    ],
)

cc_library(
    name = "call_context",
    srcs = ["call_context.cc"],
    hdrs = ["call_context.h"],
    deps = [
        "//flare/base:logging",
        "//flare/base:maybe_owning",
        "//flare/base:object_pool",
        "//flare/rpc/protocol:controller",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "call_context_factory",
    srcs = ["call_context_factory.cc"],
    hdrs = ["call_context_factory.h"],
    deps = [
        ":call_context",
        ":rpc_server_controller",
        "//flare/rpc/protocol:controller",
    ],
)

cc_library(
    name = "service_method_locator",
    srcs = ["service_method_locator.cc"],
    hdrs = ["service_method_locator.h"],
    visibility = [
        "//flare/rpc/protocol/http/builtin:__pkg__",
    ],
    deps = [
        "//flare/base:erased_ptr",
        "//flare/base:function",
        "//flare/base:logging",
        "//flare/base:never_destroyed",
        "//flare/base:type_index",
        "//flare/base/internal:annotation",
        "//flare/base/internal:hash_map",
        "//flare/init:on_init",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "service_method_locator_test",
    srcs = ["service_method_locator_test.cc"],
    deps = [
        ":service_method_locator",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "gdt_json_proto_conversion",
    srcs = ["gdt_json_proto_conversion.cc"],
    hdrs = ["gdt_json_proto_conversion.h"],
    visibility = [
        "//flare/rpc/protocol/http/builtin:__pkg__",
    ],
    deps = [
        "//flare/base:string",
        "@com_github_jsoncpp//:jsoncpp",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "gdt_json_proto_conversion_test",
    srcs = ["gdt_json_proto_conversion_test.cc"],
    deps = [
        ":gdt_json_proto_conversion",
        "//flare/testing:message_cc_proto",
        "@com_github_jsoncpp//:jsoncpp",
        "@com_google_googletest//:gtest_main",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "proto_over_http_protocol",
    srcs = ["proto_over_http_protocol.cc"],
    hdrs = ["proto_over_http_protocol.h"],
    deps = [
        # We rely on `std_protocol` to register methods to `ServiceMethodLocator`.
        ":call_context_factory",
        ":call_context",
        ":gdt_json_proto_conversion",
        ":message",
        ":rpc_meta_cc_proto",
        ":rpc_options",
        ":service_method_locator",
        ":std_protocol",
        "//flare/base:buffer",
        "//flare/base:demangle",
        "//flare/base:down_cast",
        "//flare/base:maybe_owning",
        "//flare/base:string",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol:message",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/rpc/protocol/protobuf/detail:dirty_http",
        "@com_google_protobuf//:protobuf",
    ],
    alwayslink = True,
)

cc_test(
    name = "proto_over_http_protocol_test",
    srcs = ["proto_over_http_protocol_test.cc"],
    deps = [
        ":call_context",
        ":proto_over_http_protocol",
        ":service_method_locator",
        "//flare/base:down_cast",
        "//flare/base:string",
        "//flare/init:on_init",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "proto_over_http_protocol_streaming_rpc_test",
    srcs = ["proto_over_http_protocol_streaming_rpc_test.cc"],
    deps = [
        ":protobuf",
        "//flare/base:callback",
        "//flare/base:random",
        "//flare/base/thread:latch",
        "//flare/fiber",
        "//flare/rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "std_protocol",
    srcs = ["std_protocol.cc"],
    hdrs = ["std_protocol.h"],
    deps = [
        ":call_context",
        ":call_context_factory",
        ":compression",
        ":message",
        ":rpc_meta_cc_proto",
        ":service_method_locator",
        "//flare/base:buffer",
        "//flare/base:compression",
        "//flare/base:down_cast",
        "//flare/base:endian",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol:message",
        "//flare/rpc/protocol:stream_protocol",
        "@com_google_protobuf//:protobuf",
    ],
    alwayslink = True,
)

cc_test(
    name = "std_protocol_test",
    srcs = ["std_protocol_test.cc"],
    deps = [
        ":call_context",
        ":message",
        ":service_method_locator",
        ":std_protocol",
        "//flare/init:on_init",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "std_protocol_streaming_rpc_test",
    srcs = ["std_protocol_streaming_rpc_test.cc"],
    deps = [
        ":protobuf",
        ":std_protocol",
        "//flare/fiber",
        "//flare/rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "qzone_protocol",
    srcs = [
        "qzone_protocol.cc",
    ],
    hdrs = ["qzone_protocol.h"],
    deps = [
        ":call_context",
        ":call_context_factory",
        ":message",
        ":rpc_meta_cc_proto",
        ":rpc_options",
        ":service_method_locator",
        "//flare/base:buffer",
        "//flare/base:down_cast",
        "//flare/base:overloaded",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol:message",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/rpc/protocol/protobuf/detail:qzone_header",
        "//flare/rpc/protocol/protobuf/detail:utility",
        "@com_google_protobuf//:protobuf",
    ],
    alwayslink = True,
)

cc_test(
    name = "qzone_protocol_test",
    srcs = ["qzone_protocol_test.cc"],
    deps = [
        ":call_context",
        ":message",
        ":qzone_protocol",
        ":service_method_locator",
        "//flare/init:on_init",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "qzone_protocol_streaming_rpc_test",
    srcs = ["qzone_protocol_streaming_rpc_test.cc"],
    deps = [
        ":protobuf",
        ":qzone_protocol",
        "//flare/fiber",
        "//flare/rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "rpc_client_controller",
    srcs = ["rpc_client_controller.cc"],
    hdrs = ["rpc_client_controller.h"],
    visibility = ["//flare/testing:__pkg__"],
    deps = [
        ":call_context",
        ":rpc_controller_common",
        "//flare/base:status",
        "//flare/base/internal:test_prod",
        "//flare/rpc/internal:stream_call_gate_pool",
        "//flare/rpc/protocol:controller",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_test(
    name = "rpc_client_controller_test",
    srcs = ["rpc_client_controller_test.cc"],
    deps = [
        ":rpc_channel",
        ":rpc_client_controller",
        ":rpc_server_controller",
        "//flare/base:callback",
        "//flare/base:string",
        "//flare/fiber",
        "//flare/init:override_flag",
        "//flare/rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "rpc_controller_common",
    srcs = [
        "rpc_controller_common.cc",
        "rpc_controller_common_inl.h",
    ],
    hdrs = ["rpc_controller_common.h"],
    deps = [
        ":message",
        "//flare/base:buffer",
        "//flare/base:down_cast",
        "//flare/base:status",
        "//flare/base/internal:time_view",
        "//flare/base/net:endpoint",
        "//flare/rpc/internal:stream",
        "//flare/rpc/internal:stream_call_gate",
        "//flare/rpc/internal:stream_call_gate_pool",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol/protobuf:call_context",
        "//flare/rpc/protocol/protobuf:rpc_meta_cc_proto",
    ],
)

cc_library(
    name = "rpc_server_controller",
    srcs = ["rpc_server_controller.cc"],
    hdrs = ["rpc_server_controller.h"],
    visibility = ["//flare/testing:__pkg__"],
    deps = [
        ":rpc_controller_common",
        "//flare/base:string",
        "//flare/base/internal:test_prod",
        "//flare/rpc/internal:session_context",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "rpc_server_controller_test",
    srcs = ["rpc_server_controller_test.cc"],
    deps = [
        ":rpc_channel",
        ":rpc_client_controller",
        ":rpc_server_controller",
        "//flare/fiber",
        "//flare/rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "service",
    srcs = ["service.cc"],
    hdrs = ["service.h"],
    visibility = ["//flare/rpc:__pkg__"],
    deps = [
        ":binlog",
        ":binlog_cc_proto",
        ":call_context",
        ":message",
        ":rpc_options",
        ":rpc_server_controller",
        ":service_method_locator",
        "//flare/base:buffer",
        "//flare/base:callback",
        "//flare/base:deferred",
        "//flare/base:down_cast",
        "//flare/base:maybe_owning",
        "//flare/base:string",
        "//flare/base/internal:hash_map",
        "//flare/base/internal:test_prod",
        "//flare/fiber",
        "//flare/rpc:rpc_options_cc_proto",
        "//flare/rpc/binlog",
        "//flare/rpc/internal:fast_latch",
        "//flare/rpc/internal:rpc_metrics",
        "//flare/rpc/internal:session_context",
        "//flare/rpc/internal:stream",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/rpc/protocol:stream_service",
        "//flare/rpc/tracing",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_library(
    name = "mock_channel",
    hdrs = ["mock_channel.h"],
    visibility = [
        "//flare/testing:__pkg__",
    ],
    deps = [
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "rpc_channel",
    srcs = [
        "rpc_channel.cc",
        "rpc_channel_for_dry_run.cc",
        "rpc_channel_for_dry_run.h",
    ],
    hdrs = ["rpc_channel.h"],
    deps = [
        ":binlog_cc_proto",
        ":message",
        ":mock_channel",
        ":nslb_registration",
        ":rpc_client_controller",
        ":rpc_options",
        ":service_method_locator",
        "//flare/base:callback",
        "//flare/base:chrono",
        "//flare/base:down_cast",
        "//flare/base:endian",
        "//flare/base:function",
        "//flare/base:random",
        "//flare/base:ref_ptr",
        "//flare/base:string",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/base/internal:lazy_init",
        "//flare/base/internal:test_prod",
        "//flare/base/net:endpoint",
        "//flare/fiber",
        "//flare/rpc:message_dispatcher_factory",
        "//flare/rpc/binlog",
        "//flare/rpc/internal:correlation_id",
        "//flare/rpc/internal:error_stream_provider",
        "//flare/rpc/internal:fast_latch",
        "//flare/rpc/internal:session_context",
        "//flare/rpc/internal:stream_call_gate",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/rpc/tracing",
        "@com_github_gflags_gflags//:gflags",
        "@com_github_opentracing_opentracing_cpp//:opentracing",
    ],
)

# cc_test(
#     name = "rpc_channel_test",
#     srcs = ["rpc_channel_test.cc"],
#     data = [
#         ("testdata/", "conf/"),
#     ],
#     deps = [
#         ":message",
#         ":rpc_channel",
#         "//flare/fiber",
#         "//flare/rpc",
#         "//flare/testing:echo_service_proto_flare",
#         "//flare/testing:endpoint",
#         "//flare/testing:main",
#         "//flare/testing:rpc_mock",
#     ],
# )

cc_test(
    name = "service_test",
    srcs = ["service_test.cc"],
    deps = [
        ":call_context_factory",
        ":message",
        ":rpc_server_controller",
        ":service",
        ":std_protocol",
        "//flare/fiber",
        "//flare/init:override_flag",
        "//flare/rpc/internal:stream_io_adaptor",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

[
    cc_test(
        name = "integration_test_" + block_size,
        srcs = ["integration_test.cc"],
        local_defines = ['BUFFER_BLOCK_SIZE=\\"%s\\"' % block_size],
        deps = [
            ":protobuf",
            "//flare/base:compression",
            "//flare/base:string",
            "//flare/base/thread:latch",
            "//flare/init:override_flag",
            "//flare/rpc",
            "//flare/testing:echo_service_proto_flare",
            "//flare/testing:endpoint",
            "//flare/testing:main",
        ],
    )
    for block_size in [
        "4K",
        "64K",
        "1M",
    ]
]

cc_library(
    name = "rpc_options",
    srcs = ["rpc_options.cc"],
    hdrs = ["rpc_options.h"],
    visibility = [
        "//flare/rpc/protocol/protobuf/plugin:__pkg__",
    ],
    deps = [
        "//flare/base:logging",
        "//flare/rpc:rpc_options_cc_proto",
    ],
)

proto_library(
    name = "baidu_std_rpc_meta_proto",
    srcs = ["baidu_std_rpc_meta.proto"],
)

cc_proto_library(
    name = "baidu_std_rpc_meta_cc_proto",
    deps = [":baidu_std_rpc_meta_proto"],
)

cc_library(
    name = "baidu_std_protocol",
    srcs = ["baidu_std_protocol.cc"],
    hdrs = ["baidu_std_protocol.h"],
    deps = [
        ":baidu_std_rpc_meta_cc_proto",
        ":call_context",
        ":call_context_factory",
        ":compression",
        ":message",
        ":rpc_meta_cc_proto",
        ":rpc_options",
        ":service_method_locator",
        ":std_protocol",  # For the same reason as of `proto_over_http`.
        "//flare/base:buffer",
        "//flare/base:down_cast",
        "//flare/base:endian",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol:message",
        "//flare/rpc/protocol:stream_protocol",
        "@com_github_gflags_gflags//:gflags",
    ],
    alwayslink = True,
)

cc_test(
    name = "baidu_std_protocol_test",
    srcs = ["baidu_std_protocol_test.cc"],
    deps = [
        ":baidu_std_protocol",
        ":call_context",
        ":message",
        ":service_method_locator",
        "//flare/init:on_init",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

proto_library(
    name = "poppy_rpc_meta_proto",
    srcs = ["poppy_rpc_meta.proto"],
)

cc_proto_library(
    name = "poppy_rpc_meta_cc_proto",
    deps = [":poppy_rpc_meta_proto"],
)

cc_library(
    name = "poppy_protocol",
    srcs = ["poppy_protocol.cc"],
    hdrs = ["poppy_protocol.h"],
    deps = [
        ":call_context",
        ":call_context_factory",
        ":compression",
        ":message",
        ":poppy_rpc_meta_cc_proto",
        ":rpc_meta_cc_proto",
        ":rpc_options",
        ":service_method_locator",
        ":std_protocol",  # For the same reason as of `proto_over_http`.
        "//flare/base:buffer",
        "//flare/base:down_cast",
        "//flare/base:endian",
        "//flare/base:string",
        "//flare/base/buffer:zero_copy_stream",
        "//flare/rpc/protocol:controller",
        "//flare/rpc/protocol:message",
        "//flare/rpc/protocol:stream_protocol",
        "@com_google_protobuf//:protobuf",
    ],
    alwayslink = True,
)

cc_test(
    name = "poppy_protocol_test",
    srcs = ["poppy_protocol_test.cc"],
    deps = [
        ":call_context",
        ":message",
        ":poppy_protocol",
        ":poppy_rpc_meta_cc_proto",
        ":service_method_locator",
        "//flare/base:down_cast",
        "//flare/base:endian",
        "//flare/init:on_init",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:main",
    ],
)

cc_library(
    name = "compression",
    srcs = ["compression.cc"],
    hdrs = ["compression.h"],
    deps = [
        ":message",
        ":rpc_meta_cc_proto",
        "//flare/base:buffer",
        "//flare/base:compression",
    ],
)

cc_test(
    name = "compression_test",
    srcs = ["compression_test.cc"],
    deps = [
        ":compression",
        "//flare/base:object_pool",
        "@com_google_googletest//:gtest_main",
    ],
)

proto_library(
    name = "binlog_proto",
    srcs = ["binlog.proto"],
)

cc_proto_library(
    name = "binlog_cc_proto",
    visibility = [
        "//flare/rpc/binlog:__pkg__",
    ],
    deps = ["binlog_proto"],
)

cc_library(
    name = "nslb_registration",
    srcs = ["nslb_registration.cc"],
    hdrs = [],
    deps = [
        "//flare/base:string",
        "//flare/init:on_init",
        "//flare/rpc:message_dispatcher_factory",
        "//flare/rpc/load_balancer",
        "//flare/rpc/load_balancer:round_robin",
        "//flare/rpc/message_dispatcher",
        "//flare/rpc/name_resolver",
        "//flare/rpc/name_resolver:list",
    ],
    alwayslink = True,
)
